#!/bin/bash
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Check the image database (txt files) is in sync with the list of files in
# platform2/glbench/images. The .txt files live in platform2/glbench that is
# part of the platform2 git repo while platform2/glbench/images is in its own
# repository.

GLBENCH_ROOT=$(dirname "$(readlink -f "$0")")
export LC_COLLATE=C

main() {
  local result=0
  local db
  if [[ ! -d "${GLBENCH_ROOT}/images" ]]; then
    echo "glbench-images is not synced. Skipping check." >&2
    return 0
  fi
  for db in glbench_reference_images glbench_knownbad_images; do
    find "${GLBENCH_ROOT}/images/${db}" -name '*png' -type f -printf "%f\n" |
      sort |
      diff -uN --label "a/${db}.txt" --label "b/${db}.txt" \
        "${GLBENCH_ROOT}/${db}.txt" -
    : $(( result |= $? ))
  done

  if [[ ${result} -ne 0 ]]; then
    cat >&2 <<EOF

Database files don't match the images directory. If you added or removed images
from the platform2/glbench/images directory please make sure you also update the
txt files. You can review and apply the patch generated by ./check_images.sh:

  ./check_images.sh | patch -p1

Note that platform2/glbench/images is in its own git repo so you need to submit
a different CL for changes there.
EOF
  fi
  return ${result}
}

main "$@"
